package  
{
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	import flash.utils.getTimer;
	import flash.events.Event;
	import org.flixel.FlxPath;
	import org.flixel.FlxSprite;
	import org.flixel.FlxTilemap;
	
	public class Customer extends FlxSprite
	{
		public var money : uint;
		public var hitsLeft : uint;
		public var speed : uint;
		//public var stateTimer:
		public static const STATE_ENTERINGWORLD : uint = 0;
		public static const STATE_WALKING : uint = 1;
		public static const STATE_EXITINGWORLD : uint = 2;
		public static const STATE_DEAD : uint = 3;
		
		public static const CUSTOMER_BASIC : CustomerType = new CustomerType(1, 100, 150);
		
		public var state : uint;
		public var route : Route;
		public var currentRouteNode : uint;
		
		public var stateTimer : int;
		
		public function Customer(type : CustomerType, toFollow : Route) 
		{
			super(100,100); //TODO!
			this.money = type.startingMoney;
			this.hitsLeft = type.startingHits;
			this.state = STATE_ENTERINGWORLD;
			this.speed = type.speed;
			
			var timer : Timer = new Timer(1000, 1);
			timer.addEventListener(TimerEvent.TIMER_COMPLETE, updateState);
			timer.start();
			stateTimer = getTimer();
			this.route = toFollow;
			this.currentRouteNode = 0;
		}
		
		private function updateState(event:Event = null): void
		{
			stateTimer = getTimer();
			if (state == STATE_ENTERINGWORLD)
			{
				state = STATE_WALKING;
				//Start route here!
				if (route.getWaypoints().length > 1)
				{
					var pathToFollow : FlxPath = tilemap.findPath(route.getWaypoints()[currentRouteNode].getLocation(),
						route.getWaypoints()[currentRouteNode + 1].getLocation(), true, true);
					this.followPath(pathToFollow, this.speed);
				}
			}
			else if (state == STATE_WALKING)
			{
				state = STATE_EXITINGWORLD;
				var timer : Timer = new Timer(1000, 1);
				timer.addEventListener(TimerEvent.TIMER_COMPLETE, updateState);
				timer.start();
			}
			else if (state == STATE_EXITINGWORLD)
			{
				state = STATE_DEAD;
			}
		}
		
		override public function update():void 
		{
			if (
			if (state == STATE_ENTERINGWORLD)
				this.alpha = (getTimer() - stateTimer) / 1000;
			if (state == STATE_WALKING && this.pathSpeed == 0)
				this.updateState();
			if (state == STATE_ENTERINGWORLD)
				this.alpha = (getTimer() - stateTimer) / 1000;
			super.update();
		}
	}

}